package com.dinghui.mybatis;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Authror Administrator
 * @Description:
 * @Date 2020-12-16 10:56
 * @Version: 1.0
 */
public class CodeMapperGenerator {


    public void generator() throws Exception{
        System.out.println("**************start generator***************");
        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        //指定逆向工程配置文件
        //File configFile = new File("E:\\workSpace3\\dubbo-springboot-mybatis\\mybatis-generate\\src\\main\\resources\\generatorMapperConfig.xml");
        String path = this.getClass().getClassLoader().getResource("").getPath();
        File configFile = new File(path+"/generatorMapperConfig.xml");

        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        warnings.forEach(System.out::println);
        System.out.println("*******************end generator*************");

    }

    public static void main(String[] args) throws Exception {
        try {
            CodeMapperGenerator generatorSqlmap = new CodeMapperGenerator();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*public static void main(String[] args) {
        try {
            System.out.println("**************start generator***************");
            List<String> warnings = new ArrayList<>();
            boolean overwrite = true;
            //指定逆向工程配置文件
            //File configFile = new File("E:\\workSpace3\\dubbo-springboot-mybatis\\mybatis-generate\\src\\main\\resources\\generatorConfig.xml");
            //File configFile = new File("src\\main\\resources\\generatorMapperConfig.xml");
            String path = this.getClass().getClassLoader().getResource("").getPath();
            File configFile = new File(path+"/generatorMapperConfig.xml");

            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
            warnings.forEach(System.out::println);
            System.out.println("*******************end generator*************");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

}
